package com.joolun.cloud.gateway;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * @author eric
 * @describe
 * @email 893199207@qq.com
 * @date 2021/12/15 17:53
 */
public class Test {
	public static void main(String[] args) throws NacosException, InterruptedException {
		String serverAddr = "nacos.dev.nxp.51xpx.com:8848";
		String dataId = "dynamic_routes.yml";
		String group = "DEFAULT_GROUP";
		Properties properties = new Properties();
		properties.put("serverAddr", serverAddr);
		//从工厂方法获取ConfigService
		ConfigService configService = NacosFactory.createConfigService(properties);
		String content = configService.getConfig(dataId, group, 5000);
		System.out.println("first:"+content);
		//添加配置监听
		configService.addListener(dataId, group, new Listener() {
			@Override
			public void receiveConfigInfo(String configInfo) {
				System.out.println("receive:" + configInfo);
			}

			@Override
			public Executor getExecutor() {
				return null;
			}
		});

//		//发布配置
//		boolean isPublishOk = configService.publishConfig(dataId, group, "content");
//		System.out.println(isPublishOk);

		//获取配置
		Thread.sleep(3000);
		content = configService.getConfig(dataId, group, 5000);
		System.out.println(content);

		//删除配置
//		boolean isRemoveOk = configService.removeConfig(dataId, group);
//		System.out.println(isRemoveOk);
//		Thread.sleep(3000);

		//再次获取配置
		content = configService.getConfig(dataId, group, 5000);
		System.out.println(content);
		Thread.sleep(300000);
	}

}
